package com.parttimetask.activities.main;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.parttimetask.activities.AboutActivity;
import com.parttimetask.App;
import com.parttimetask.activities.AdminActivity;
import com.parttimetask.bean.ExcelBean;
import com.parttimetask.utils.ExcelUtil;
import com.parttimetask.activities.FeedbackActivity;
import com.parttimetask.activities.HelpCenterActivity;
import com.parttimetask.utils.Http;
import com.parttimetask.activities.LoginActivity;
import com.parttimetask.databinding.FragmentMineBinding;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MineFragment extends Fragment {
    private FragmentMineBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMineBinding.inflate(inflater, container, false);
        binding.mineExit.setOnClickListener(view -> {
            AlertDialog.Builder dialog=new AlertDialog.Builder(requireActivity());
            dialog.setMessage("Log out?").setPositiveButton("confirm", (dialogInterface, i) -> {
                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                edit.putString("mobile","null");
                edit.putString("password","null");
                edit.apply();
                startActivity(new Intent(requireActivity(),LoginActivity.class));
                requireActivity().finish();
            }).setNegativeButton("cancel",null).show();
        });

        binding.mineFeedback.setOnClickListener(view -> startActivity(new Intent(requireActivity(), FeedbackActivity.class)));
        binding.mineHelpCenter.setOnClickListener(view -> startActivity(new Intent(requireActivity(), HelpCenterActivity.class)));
        binding.mineServiceBox.setOnClickListener(view -> {
            AlertDialog.Builder dialog=new AlertDialog.Builder(requireActivity());
            dialog.setTitle("Email").setMessage("parttimeearn-in@outlook.com").setPositiveButton("ok",null).show();
        });
        binding.mineAbout.setOnClickListener(view -> startActivity(new Intent(requireActivity(), AboutActivity.class)));
        binding.mineAdmin.setOnClickListener(view -> {
            AlertDialog.Builder dialog=new AlertDialog.Builder(requireActivity());
            EditText et=new EditText(requireActivity());
            et.setHint("administrator's password");
            dialog.setTitle("verity").setView(et).setPositiveButton("confirm", (dialogInterface, i) -> {
               if (et.getText().toString().equals("wang000852")){
                   startActivity(new Intent(requireActivity(), AdminActivity.class));
               }
            }).setNegativeButton("cancel",null).show();
        });
        Http.get("user/query/?mobile=" + App.sp.getString("mobile", ""), new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                requireActivity().runOnUiThread(() -> {
                    try {
                        if (new JSONObject(response.body().string()).getBoolean("isAdmin")){
                         binding.mineAdmin.setVisibility(View.VISIBLE);
                        }else{
                            binding.mineAdmin.setVisibility(View.GONE);
                        }
                    } catch (Exception e) {
                        binding.mineAdmin.setVisibility(View.GONE);
                    }
                });
            }
        });
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}